package com.candoit.webapps.pricing.controllers;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.candoit.webapps.pricing.model.Articulo;
import com.candoit.webapps.pricing.model.Linea;
import com.candoit.webapps.pricing.model.MateriaPrima;
import com.candoit.webapps.pricing.util.CalculoCostosUtil;
import com.candoit.webapps.pricing.util.MateriaPrimaArticuloUtil;
import com.candoit.webapps.pricing.util.QueryUtil;


@ManagedBean(name = "calculoCtrl")
@ViewScoped
public class CalculosCostosController extends AbstractController {
	 private Linea lineaSelected = null;
	 private List<CalculoCostosUtil> listCosts  = null;

	public Linea getLineaSelected() {
		return lineaSelected;
	}

	public void setLineaSelected(Linea lineaSelected) {
		this.lineaSelected = lineaSelected;
	}

	public List<CalculoCostosUtil> getListCosts() {
		if (listCosts == null){
			this.fillCostos();
		}
		return listCosts;
	}

	public void setListCosts(List<CalculoCostosUtil> listCosts) {
		this.listCosts = listCosts;
	}
	
	private void fillCostos(){
		listCosts = new ArrayList<CalculoCostosUtil>();
		if (lineaSelected == null){
			List<Articulo> articulos = QueryUtil.getInstance().getArticulos();
			Iterator<Articulo> it = articulos.iterator();
			while (it.hasNext()){
				   Articulo articulo = (Articulo)it.next();
				   CalculoCostosUtil util = new CalculoCostosUtil();
				   util.setArticulo(articulo);
				   util.setMedida(articulo.getFormato().getMedida());
				   util.setMateriasPrimas(new ArrayList<MateriaPrimaArticuloUtil>());
				   List<MateriaPrima> materias = QueryUtil.getInstance().getMateriaPrimaFilter();
				   Iterator<MateriaPrima> it2 = materias.iterator();
				   while (it2.hasNext()){
					      MateriaPrima mat = (MateriaPrima) it2.next();
					      MateriaPrimaArticuloUtil matUtil = new MateriaPrimaArticuloUtil();
					      matUtil.setMp(mat);
					      matUtil.setCostoActual(mat.getCostoUnitario());
					      matUtil.setCostoSimulado(0.00d);
					      util.getMateriasPrimas().add(matUtil);
				   }
				   listCosts.add(util);
			}
		}
	}
	
	
	public void buscar(){
		
	}
	
	 
}
